package com.ohos.carbon.slice.themes;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class DarkAndLightSlice extends AbilitySlice {

    private Image dark_image, light_image, check_dark, check_light, radio_dark, radio_light;
    private Text dark_title_text, light_title_text;

    private DirectionalLayout layout_check1, layout_check2, layout_radio1, layout_radio2;

    private boolean flag_layout = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_dark_and_light);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Dark and Light")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Dark and Light")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Dark and Light", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Dark and Light", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Dark and Light", 1);
            pf.flushSync();
        }

    }

    private void initView() {
        dark_image = (Image) findComponentById(ResourceTable.Id_dark_image);
        light_image = (Image) findComponentById(ResourceTable.Id_light_image);

        dark_title_text = (Text) findComponentById(ResourceTable.Id_dark_title_text);
        light_title_text = (Text) findComponentById(ResourceTable.Id_light_title_text);

        dark_title_text.setText("Dark theme");
        light_title_text.setText("Light theme");
        dark_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow_white));
        light_image.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        //menu_back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        initCheckbox();
        dark_image.setClickedListener(component -> onBackPressed());
        light_image.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                onBackPressed();
            }
        });
    }

    private void initCheckbox() {
        layout_check1 = (DirectionalLayout) findComponentById(ResourceTable.Id_layout_check1);
        layout_check2 = (DirectionalLayout) findComponentById(ResourceTable.Id_layout_check2);
        layout_radio1 = (DirectionalLayout) findComponentById(ResourceTable.Id_layout_radio1);
        layout_radio2 = (DirectionalLayout) findComponentById(ResourceTable.Id_layout_radio2);
        check_dark = (Image) findComponentById(ResourceTable.Id_check_dark);
        check_light = (Image) findComponentById(ResourceTable.Id_check_light);
        radio_dark = (Image) findComponentById(ResourceTable.Id_radio_dark);
        radio_light = (Image) findComponentById(ResourceTable.Id_radio_light);


        layout_check1.setClickedListener(component -> {
            if (check_dark != null && flag_layout == false) {
                check_dark.setPixelMap(ResourceTable.Media_check_green_on);
                flag_layout = true;
            } else {
                check_dark.setPixelMap(ResourceTable.Media_check_dark_off);
                flag_layout = false;
            }
        });
        layout_check2.setClickedListener(component -> {
            if (check_light != null && flag_layout == false) {
                check_light.setPixelMap(ResourceTable.Media_check_green_on);
                flag_layout = true;
            } else {
                check_light.setPixelMap(ResourceTable.Media_check_light_off);
                flag_layout = false;
            }
        });
        layout_radio1.setClickedListener(component -> {
            if (radio_dark != null && flag_layout == false) {
                radio_dark.setPixelMap(ResourceTable.Media_radio_green_on);
                flag_layout = true;
            } else {
                //fender_checkbox.setPixelMap(ResourceTable.Media_check_dark_off);
                flag_layout = false;
            }
        });
        layout_radio2.setClickedListener(component -> {
            if (radio_light != null && flag_layout == false) {
                radio_light.setPixelMap(ResourceTable.Media_radio_green_on);
                flag_layout = true;
            } else {
                //fender_checkbox.setPixelMap(ResourceTable.Media_check_dark_off);
                flag_layout = false;
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
